package com.itsq.service.resources.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itsq.common.bean.ErrorEnum;
import com.itsq.common.constant.APIException;
import com.itsq.mapper.BannerMapper;
import com.itsq.mapper.GpsMapper;
import com.itsq.pojo.dto.GpsDto;
import com.itsq.pojo.dto.LocationServiceDto;
import com.itsq.pojo.dto.PageLocationDto;
import com.itsq.pojo.entity.Banner;
import com.itsq.pojo.entity.Car;
import com.itsq.pojo.entity.Gps;
import com.itsq.service.resources.LocationService;
import com.itsq.utils.wx.HttpClientUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.misc.Request;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 史先帅
 * @since 2021-01-29
 */
@Service
public class LocationServiceImpl implements LocationService {

    @Autowired(required = false)
    private GpsMapper gpsMapper;

    //令牌
    String mds="";
    //单位id
    String loginId="";
    //设备id
    String userId="";

    @Override
    public Page<Gps> selLocationPage(PageLocationDto pageLocationDto) {
        Page<Gps> page=new Page<>(pageLocationDto.getPageIndex(),pageLocationDto.getPageSize());
        QueryWrapper queryWrapper=new QueryWrapper();
        if(pageLocationDto.getOrderByField()!=null){
            queryWrapper.orderByDesc(pageLocationDto.getOrderByField());
        }
        return gpsMapper.selectPage(page,queryWrapper);
    }

    @Override
    public Integer update(Gps gps) {
        int i = gpsMapper.updateById(gps);
        if(i<1){
            throw new APIException(ErrorEnum.is_UPDATE);
        }
        return i;
    }

    @Override
    public Integer delete(Integer id) {
        return gpsMapper.deleteById(id);
    }

    @Override
    public int loginSystem() {
        LocationServiceDto locationServiceDto=new LocationServiceDto();
        //GPS登录访问地址
        String url="http://apitest.18gps.net/GetDateServices.asmx/loginSystem?LoginName=cyxx&LoginPassword=cyxx168&LoginType=ENTERPRISE&language="+locationServiceDto.getLanguage()+"&ISMD5="+locationServiceDto.getISMD5()+"&timeZone="+locationServiceDto.getTimeZone()+"&apply="+locationServiceDto.getApply()+"&loginUrl="+locationServiceDto.getLoginUrl();
        //访问结果
        String s = HttpClientUtil.doGet(url);
        //json转换成map
        Map map = (Map) JSON.parse(s);
        //获取令牌
        mds=(String) map.get("mds");
        //获取单位id
        loginId =(String) map.get("id");
        //获取登录结果
        String su = (String) map.get("success");
        if(su.equals("true")){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int loginSystemUser(String loginName) {
        LocationServiceDto locationServiceDto=new LocationServiceDto();
        //GPS登录访问地址
        String url="http://apitest.18gps.net/GetDateServices.asmx/loginSystem?LoginName="+loginName+"&LoginPassword=123456&LoginType=USER&language="+locationServiceDto.getLanguage()+"&ISMD5="+locationServiceDto.getISMD5()+"&timeZone="+locationServiceDto.getTimeZone()+"&apply="+locationServiceDto.getApply()+"&loginUrl="+locationServiceDto.getLoginUrl();
        //访问结果
        String s = HttpClientUtil.doGet(url);
        //json转换成map
        Map map = (Map) JSON.parse(s);
        //获取令牌
        mds=(String) map.get("mds");
        //获取单位id
        userId =(String) map.get("id");
        //获取登录结果
        String su = (String) map.get("success");
        if(su.equals("true")){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public int bindGPS(String macid,String platenumber,String fullName) {
        loginSystem();
        //绑定gps地址
        String url="http://apitest.18gps.net//GetDateServices.asmx/GetDate?method=ActiveAndAddDevice&login_id="+loginId+"&mds="+mds+"&FullName="+fullName+"&Macid="+macid;
        //访问结果
        String s = HttpClientUtil.doGet(url);
        //json转换成map
        Map map = (Map) JSON.parse(s);
        //获取绑定结果
        String su = (String) map.get("success");
        if(su.equals("true")){
            Gps gps=new Gps();
            gps.setFullName(fullName);
            gps.setPlatenumber(platenumber);
            gps.setMacid(macid);
            int insert = gpsMapper.insert(gps);
            if(insert<1){
                throw new APIException(ErrorEnum.is_ISNULL);
            }
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public String getGPS(Double lat, Double lon) {
        //获取经纬度
        String url="http://poi.18gps.net/POI?lat="+lat+"&lon="+lon+"&map=BAIDU";
        //访问结果
        String s = HttpClientUtil.doGet(url);
        return s;
    }

    @Override
    public String getUserAndGpsInfoByIDsUtc(String loginName) {
        loginSystemUser(loginName);
        //获取单个最新位置地址
        String url="http://apitest.18gps.net/GetDateServices.asmx/GetDate?mapType=BAIDU&mds="+mds+"&method=getUserAndGpsInfoByIDsUtc&option=cn&user_id="+userId;
        //访问结果
        String s = HttpClientUtil.doGet(url);
        //转换json对象
        JSONObject json = JSONObject.parseObject(s);
        //获取records结果
        Object key = json.get("records");
        //转换成list
        List list=(List)key;
        //转换成string
        String l = list.get(0).toString();
        //去除中括号
        l = l.replace("[","");
        l = l.replace("]","");
        //按照都好转换数组
        String[] l2=l.split(",");
        //获取经度
        String lat = l2[2];
        //获取维度
        String lon = l2[3];
        //调用经纬度方法获取具体地址
        String gps = lat + "," + lon;
        return gps;
    }



}
